home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 3 / Gold Medal Software - Volume 3 (Gold Medal) (1994).iso / prog / tge131.arj / MAKEFONT.C < prev    next >
C/C++ Source or Header  |  1994-03-05  |  5KB  |  181 lines

  1. /**************************************************************************
  2. *  File:        MAKEFONT.C     Copyright (c) 1993-1994 by Matthew Hildebrand
  3. *
  4. *  Purpose:     Make a font file from its component RAW files.
  5. **************************************************************************/
  6.  
  7. #include <alloc.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. void readOffsetFile(char *filename);
  13. void readPaletteFile(char *filename);
  14.  
  15. unsigned short offsets[256];
  16. unsigned char palette[768];
  17. const char copyright[] = " Copyright (c) 1993-1994 by Matthew Hildebrand.  All rights reserved.";
  18.  
  19.  
  20.  
  21. /****
  22. ***** Program entry point.
  23. ****/
  24.  
  25. void main(int argc, char *argv[])
  26. {
  27.   FILE *inFile, *outFile;
  28.   char filename[80];
  29.   void far *charAddr=NULL;
  30.   int count;
  31.   unsigned short zero=0;
  32.   unsigned long fileSize;
  33.  
  34.   /*** Ensure filename specified ***/
  35.   printf("MAKEFONT 1.31  Copyright (c) 1993-1994 by Matthew Hildebrand\n\n");
  36.   if (argc < 2)
  37.   {
  38.     printf("        Usage:  MAKEFONT fontname\n\n");
  39.     exit(EXIT_FAILURE);
  40.   }
  41.  
  42.   /*** Read the offsetFromTop data file ***/
  43.   strcpy(filename, argv[1]);
  44.   strcat(filename, ".off");
  45.   strupr(filename);
  46.   readOffsetFile(filename);
  47.  
  48.   /*** Read the palette file ***/
  49.   strcpy(filename, argv[1]);
  50.   strcat(filename, ".pal");
  51.   strupr(filename);
  52.   readPaletteFile(filename);
  53.  
  54.   /*** Open the output file ***/
  55.   strcpy(filename, argv[1]);
  56.   strcat(filename, ".fnt");
  57.   strupr(filename);
  58.   if ((outFile=fopen(filename,"wb")) == NULL)
  59.   {
  60.     printf("Error opening file '%s', aborting.\n\n", filename);
  61.     exit(EXIT_FAILURE);
  62.   }
  63.  
  64.   /*** Write the font file header ***/
  65.   fwrite("TGEFONT2", 8, 1, outFile);                    /* signature */
  66.   fwrite(copyright, strlen(copyright)+1, 1, outFile);   /* copyright notice */
  67.   fwrite(palette, 768, 1, outFile);                     /* font palette */
  68.  
  69.   /*** Copy the character data, one character at a time ***/
  70.   for (count=0; count<256; count++)
  71.   {
  72.     /*** Write the character's offsetFromTop entry ***/
  73.     fwrite(&offsets[count], sizeof(short), 1, outFile);
  74.  
  75.     /*** Try to open the character file ***/
  76.     sprintf(filename, "%d.raw", count);
  77.     if ((inFile=fopen(filename,"rb")) != NULL)  /* file exists */
  78.     {
  79.       /*** Allocate memory for the character file ***/
  80.       fseek(inFile, 0L, SEEK_END);          /* seek to end-of-file */
  81.       fileSize = ftell(inFile);             /* get file size */
  82.       rewind(inFile);                       /* seek to start-of-file */
  83.       if (charAddr != NULL)                 /* free old memory if necessary */
  84.         farfree(charAddr);
  85.       charAddr = farmalloc(fileSize);       /* allocate new memory */
  86.  
  87.       /*** Read in the character data ***/
  88.       if (!fread(charAddr, (size_t)fileSize, 1, inFile))
  89.       {
  90.         printf("Error reading file '%s', aborting.\n\n", filename);
  91.         exit(EXIT_FAILURE);
  92.       }
  93.  
  94.       /*** Write the data to the new file ***/
  95.       if (!fwrite(charAddr, (size_t)fileSize, 1, outFile))
  96.       {
  97.         printf("Error writing file '%s', aborting.\n\n", filename);
  98.         exit(EXIT_FAILURE);
  99.       }
  100.       fclose(inFile);
  101.     }
  102.     else                                        /* file doesn't exist */
  103.     {
  104.       /*** Write a blank character entry ***/
  105.       if (!fwrite(&zero, sizeof(short), 1, outFile))  /* zero width */
  106.       {
  107.         printf("Error writing file '%s', aborting.\n\n", filename);
  108.         exit(EXIT_FAILURE);
  109.       }
  110.       if (!fwrite(&zero, sizeof(short), 1, outFile))  /* zero depth */
  111.       {
  112.         printf("Error writing file '%s', aborting.\n\n", filename);
  113.         exit(EXIT_FAILURE);
  114.       }
  115.     }
  116.   }
  117. }
  118.  
  119.  
  120.  
  121. /****
  122. ***** Read the characters' offset data.
  123. ****/
  124.  
  125. void readOffsetFile(char *filename)
  126. {
  127.   FILE *offFile;
  128.   int count;
  129.   char curLine[80];
  130.  
  131.   /*** Open the file ***/
  132.   if ((offFile=fopen(filename,"rb")) == NULL)
  133.   {
  134.     printf("Error opening file '%s', aborting.\n\n", filename);
  135.     exit(EXIT_FAILURE);
  136.   }
  137.  
  138.   /*** Read in the offsets, one by one ***/
  139.   for (count=0; count<256; count++)
  140.   {
  141.     /*** Read the line ***/
  142.     if (fgets(curLine, 80, offFile) == NULL)
  143.     {
  144.       printf("Error reading file '%s', aborting.\n\n", filename);
  145.       exit(EXIT_FAILURE);
  146.     }
  147.     /*** Convert the text to a number ***/
  148.     offsets[count] = atoi(curLine);
  149.     if (offsets <= 0)
  150.     {
  151.       printf("Error on line %d of file '%s', aborting.\n\n", filename, count);
  152.       exit(EXIT_FAILURE);
  153.     }
  154.   }
  155. }
  156.  
  157.  
  158.  
  159. /****
  160. ***** Read the font's palette data file.
  161. ****/
  162.  
  163. void readPaletteFile(char *filename)
  164. {
  165.   FILE *palFile;
  166.  
  167.   /*** Open the file ***/
  168.   if ((palFile=fopen(filename,"rb")) == NULL)
  169.   {
  170.     printf("Error opening file '%s', aborting.\n\n", filename);
  171.     exit(EXIT_FAILURE);
  172.   }
  173.  
  174.   /*** Read the data ***/
  175.   if (!fread(palette, 768, 1, palFile))
  176.   {
  177.     printf("Error reading file '%s', aborting.\n\n", filename);
  178.     exit(EXIT_FAILURE);
  179.   }
  180. }
  181.